﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;

namespace BLL
{
    public class TinTucBLL: BaseBLL
    {

        public List<TinTuc> LayTatCa()
        {
            return DB.TinTucs.OrderByDescending(tt => tt.ThoiDiem).ToList();
        }

        public List<TinTuc> LayTop(int sotin)
        {
            return DB.TinTucs.OrderByDescending(tt => tt.ThoiDiem).Take(sotin).ToList();
        }

        public TinTuc LayTheoMa(int ma)
        {
            return DB.TinTucs.Where(tt => tt.Ma==ma).FirstOrDefault();
        }

        public TinTuc Them(int maloaitt, string tieude, string mota, string noidung, string hinhanh)
        {
            TinTuc tt = new TinTuc()
            {
                MaLoaiTT=maloaitt,
                TieuDe=tieude,
                MoTa=mota,
                NoiDung=noidung,
                ThoiDiem = DateTime.Now,
                HinhAnh=hinhanh
            };

            DB.TinTucs.InsertOnSubmit(tt);
            DB.SubmitChanges();
            return tt;
        }

        public void Sua(int ma, int maloaitt, string tieude, string mota, string noidung, string hinhanh)
        {
            TinTuc tt = LayTheoMa(ma);
            tt.MaLoaiTT = maloaitt;
            tt.TieuDe = tieude;
            tt.MoTa = mota;
            tt.NoiDung = noidung;
            tt.HinhAnh = hinhanh;

            DB.SubmitChanges();
        }

        public void Xoa(int ma)
        {
            TinTuc tt = LayTheoMa(ma);
            DB.TinTucs.DeleteOnSubmit(tt);

            DB.SubmitChanges();
        }

        public List<TinTuc> LayTinMoi(int sotin,DateTime dt)
        {
            return DB.TinTucs.Where(tt => tt.ThoiDiem > dt).OrderBy(tt2 => tt2.ThoiDiem).Take(sotin).ToList();
        }

        public List<TinTuc> LayTinCu(int sotin, DateTime dt)
        {
            return DB.TinTucs.Where(tt => tt.ThoiDiem < dt).OrderByDescending(tt2 => tt2.ThoiDiem).Take(sotin).ToList();
        }

    }
}
